const User = require('../model/user.model');

class UserService {

  async createUser(user_name,password,role_id){
    console.log(role_id)
    return await User.create({user_name, password, role_id})

     }

  async getUserInfo({
    user_name,
    is_admin,
    id
  }){
    const whereOpt = {};
    id && Object.assign(whereOpt,{id});
    is_admin && Object.assign(whereOpt,{is_admin})
    user_name && Object.assign(whereOpt,{user_name})

    const res = await User.findOne({
      attributes:['id','user_name','password','is_admin',"role_id"],
      where:whereOpt
    })

    return res ? res.dataValues : null

  }
  async getUsers(limit,page) {
    return await User.findAndCountAll({
      offset: (Number(page)-1)*10,
      limit: Number(limit)
    })
  }

}
module.exports =new UserService()

